import utils from "../../utils/index";
import {rankModel} from "../../apis/index"

import regeneratorRuntime from '../../libs/wxPromise.min';
import {
  InterAd
} from '../../utils/InterAd'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    tabIndex: 0,
    bannerList:[], //banner列表
    page:{
      page:1,
      pageSize:10
    },
    myIdols:[]
  },
  mixins: [require('../../myMixin.js')],
  onLoad: function (options) {
    let {tabIndex} = options
    tabIndex = tabIndex?tabIndex:0
    this.setData({
      tabIndex:tabIndex
    })
    this.getBannerList()
    this.getShareInfo()
  },
  
  async onShow () {
    utils.showLoading()
    let groups =await rankModel.groupList()
    let gList = []
    groups.forEach(el => {
        let tmp = {
          id:el.id,
          name:el.group_name
        }
        gList.push(tmp)
    });
    this.setData({
      list:gList
    },()=>{
      let {list,tabIndex} = this.data
      let id = list[tabIndex].id
      this.getStarList(id)
    })
    let myIdols = await rankModel.myIdol()
    this.setData({
      myIdols:myIdols
    })
  },
  onShareAppMessage: function () {
    let {text,image} = this.data.share_data
    return {
      title:text,
      imageUrl:image
    }
  },
  onFocus(){
    utils.goUrl(`../search/search`)
  },
  tabClick(e){
    let index = e.detail.index
    this.data.tabIndex = index
    this.getInitStarList()
  },
  goUrl(e){
    let url = e.currentTarget.dataset.url
    utils.goUrl(url)
  },
  getBannerList(){
    rankModel.bannerList().then(res=>{
      this.setData({
        bannerList:res
      })
    })
  },
  onReady: function () {
    let interAd = new InterAd('7fb828e435af61d59234ae3978c20221')
    interAd.showAd()
  },
  onReachBottom(){
    this.moreList1Data()
  },
  getInitStarList(){
    let {starList,page} = this.data
    starList = []
    page.page = 1
    this.data.starList = starList
    this.data.page = page
    this.getStarList()
  },
  getStarList(){
    let {page,pageSize} = this.data.page
    let {starList=[],list,tabIndex} = this.data
    let param = {
      page:page,
      pageSize:pageSize,
      gid:list[tabIndex].id
    }
    utils.showLoading()
    rankModel.starList(param).then(res=>{
      let _list = res.list
      starList = starList.concat(_list)
      this.computedPage('page',res.total)
      this.setData({
        starList:starList
      })  
      utils.hideAll()
    }).catch(()=>{
      utils.hideAll()
    })
  },
  moreList1Data(){
    this.loadMoreData('page',this.getStarList)
  },
  goLink(e){
    let item = e.currentTarget.dataset.item
    if(item.link==null){
      return
    }
    if(item.link.indexOf('http')>-1||item.link.indexOf('www')>-1){
      utils.prefix.navigateTo({
        url: `../webview/index?url=${item.link}`,
        success: (result)=>{
          
        },
        fail: ()=>{},
        complete: ()=>{}
      });
    }else{
      utils.prefix.navigateToMiniProgram({
        appId:item.link,
        path:''
      });
    }
  }
})